package com.sjgtw.cloud.app.controller;

import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.exception.BaseException;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.service.SystemServerService;
import com.sjgtw.cloud.system.api.model.LoginUser;
import com.sjgtw.cloud.web.waterDamage.domain.WaterDamage;
import com.sjgtw.cloud.web.waterDamage.service.IWaterDamageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 水害台账Controller
 * 
 * @author lvyoufang
 * @date 2024-03-12
 */
@RestController
@RequestMapping("/app/waterDamage")
@Api(tags = "6.2.2、APP水害台账")
public class AppWaterDamageController extends BaseController
{
    @Autowired
    private IWaterDamageService waterDamageService;

    @Autowired
    private SystemServerService systemServerService;

    @Autowired
    private RedisService redisService;

    /**
     * 查询水害台账列表
     */
    @ApiOperation(value = "查询水害台账列表")
    @GetMapping("/list")
    public TableDataInfo list(WaterDamage waterDamage)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);
        if (user == null) {
            throw new BaseException("登录信息失效，请重新登录");
        }
        waterDamage.setCreateUserId(user.getUserid());
      /*  if (user != null) {
            AjaxResult ajaxResult = systemServerService.getUserDeptLevel(user.getUserid());
            if (ajaxResult != null) {
                Object object = ajaxResult.get("data");
                if (object != null) {
                    JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(object));
                    String dept = jsonObject.getString("dept");
                    Long deptId= jsonObject.getLong("deptId");
                    Long parentId= jsonObject.getLong("parentId");
                    if("section".equals(dept)){
                        waterDamage.setSection(deptId);
                    }else if("workArea".equals(dept)){
                        waterDamage.setSection(parentId);
                        waterDamage.setWorkArea(deptId);
                    }
                }

            }
        }*/



     /*   List<Long> deptIds = new ArrayList<>();
        if (user != null) {
            AjaxResult ajaxResult = systemServerService.getSubDeptIdsByUserId(user.getUserid());
            if (ajaxResult != null) {
                Object object = ajaxResult.get("data");
                if (object != null) {
                    deptIds = (List<Long>) object;
                }
            }
        }
        waterDamage.setDeptIds(deptIds);*/

        startPage();
        List<WaterDamage> list = waterDamageService.selectWaterDamageList(waterDamage);
        return getDataTable(list);
    }

    /**
     * 获取水害台账详细信息
     */
    @ApiOperation(value = "获取水害台账详细信息")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long id)
    {
        return AjaxResult.success(waterDamageService.selectWaterDamageById(id));
    }

    /**
     * 新增水害台账
     */
    @ApiOperation(value = "新增水害台账")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WaterDamage waterDamage)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);
        if (user == null) {
            throw new BaseException("登录信息失效，请重新登录");
        }
        waterDamage.setCreateUserId(user.getUserid());
     /*   if (user != null) {
            AjaxResult ajaxResult = systemServerService.getUserDeptLevel(user.getUserid());
            if (ajaxResult != null) {
                Object object = ajaxResult.get("data");
                if (object != null) {
                    JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(object));
                    String dept = jsonObject.getString("dept");
                    Long deptId= jsonObject.getLong("deptId");
                    Long parentId= jsonObject.getLong("parentId");
                    if("section".equals(dept)){
                        waterDamage.setSection(deptId);
                    }else if("workArea".equals(dept)){
                        waterDamage.setSection(parentId);
                        waterDamage.setWorkArea(deptId);
                    }
                }

            }
        }*/

        return toAjax(waterDamageService.insertWaterDamage(waterDamage));
    }

    /**
     * 修改水害台账
     */
    @ApiOperation(value = "修改水害台账")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody WaterDamage waterDamage)
    {
        return toAjax(waterDamageService.updateWaterDamage(waterDamage));
    }

    /**
     * 删除水害台账
     */
    @ApiOperation(value = "删除水害台账")
	@PostMapping("/remove")
    public AjaxResult remove(String[] ids)
    {
        return toAjax(waterDamageService.deleteWaterDamageByIds(ids));
    }
}
